package com.healthtop.service.impl;

import com.healthtop.common.Specification;
import com.healthtop.dao.XsArticleCategoryDao;
import com.healthtop.dto.XsArticleCategoryDto;
import com.healthtop.model.XsArticleCategory;
import com.healthtop.model.XsArticleCategoryExample;
import com.healthtop.model.XsGameCategory;
import com.healthtop.service.XsArticleCategoryService;
import com.healthtop.service.XsGameGategoryService;
import com.healthtop.utils.UserUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class XsArticleCategoryServiceImpl implements XsArticleCategoryService {

    @Autowired
    private XsArticleCategoryDao xsArticleCategoryDao;

    @Autowired
    private XsGameGategoryService xsGameGategoryService;

    @Override
    public XsArticleCategoryDto forward(XsArticleCategoryDto xsArticleCategoryDto) {
        if(xsArticleCategoryDto.getMode()== Specification.OperateMode.UPDATE.index){
            XsArticleCategory xsArticleCategory = xsArticleCategoryDao.selectById(xsArticleCategoryDto.getId());
            BeanUtils.copyProperties(xsArticleCategory,xsArticleCategoryDto);
        }
        List<XsGameCategory> list = xsGameGategoryService.selectAll();
        xsArticleCategoryDto.setList(list);
        return xsArticleCategoryDto;
    }

    @Override
    public void operate(XsArticleCategoryDto xsArticleCategoryDto) {
        XsArticleCategory xsArticleCategory = new XsArticleCategory();
        BeanUtils.copyProperties(xsArticleCategoryDto,xsArticleCategory);
        if(xsArticleCategoryDto.getMode()== Specification.OperateMode.CREATE.index){
            xsArticleCategory.setCreateTime(new Date());
            xsArticleCategory.setDelFalg("0");
            xsArticleCategory.setCreateUser(UserUtils.getCurrentUser().getUid());
            xsArticleCategory.setUpdateTime(new Date());
            xsArticleCategoryDao.insert(xsArticleCategory);
        }else{
            xsArticleCategory.setUpdateUser(UserUtils.getCurrentUser().getUid());
            xsArticleCategory.setUpdateTime(new Date());
            xsArticleCategoryDao.update(xsArticleCategory);
        }
    }

    @Override
    public void delete(XsArticleCategoryDto xsArticleCategoryDto) {
        XsArticleCategory xsArticleCategory = new XsArticleCategory();
        xsArticleCategory.setId(xsArticleCategoryDto.getId());
        xsArticleCategory.setDelFalg("1");
        xsArticleCategory.setUpdateUser(UserUtils.getCurrentUser().getUid());
        xsArticleCategory.setUpdateTime(new Date());
        xsArticleCategoryDao.update(xsArticleCategory);
    }

    @Override
    public List<XsArticleCategory> selectByGameId(Long gameId) {
        return xsArticleCategoryDao.selectByGameId(gameId);
    }

}
